package action;

	import java.util.Date;
	
	
	import persistency.PersistencyException;
	import persistency.Price_CRUD;
	import persistency.Product_CRUD;
	import model.Price;
import com.opensymphony.xwork2.ActionSupport;
	
@SuppressWarnings("serial")
public class RegisterPriceExecute extends ActionSupport{
		
	private Price prijs;
	private String active;
	private String unitCode;
	private String productId;
	private String editId;
	private Price_CRUD crud = new Price_CRUD();
	private Product_CRUD prodCrud = new Product_CRUD();
	private Price priceBean;
	
	
	public Price getPriceBean() {
		return priceBean;
	}

	public void setPriceBean(Price priceBean) {
		this.priceBean = priceBean;
	}

	public String getActive() {
			return active;
		}

		public void setActive(String active) {
			this.active = active;
		}

		public String getUnitCode() {
			return unitCode;
		}

		public void setUnitCode(String unitCode) {
			this.unitCode = unitCode;
		}

		public String getProductId() {
			return productId;
		}

		public void setProductId(String productId) {
			this.productId = productId;
		}

		private String price;
		public String getPrice() {
			return price;
		}

		public void setPrice(String price) {
			this.price = price;
		}

		public Price getPrijs() {
			return prijs;
		}

		public void setPrijs(Price prijs) {
			this.prijs = prijs;
		}
		
		public String getEditId() {
			return editId;
		}

		public void setEditId(String editId) {
			this.editId = editId;
		}
		
		public RegisterPriceExecute() throws PersistencyException{
			
		}
		
		
		@Override
		public String execute() throws Exception {
			try{
				System.out.println(editId);
			if (editId.equals(" ")) {	
		
			//call Service class to store priceBean's state in database
			//hier moet het submitten van de informatie naar de DB gebeuren.
				priceBean = new Price();
				priceBean.setPrice(Double.parseDouble(price));
			priceBean.setActive(Boolean.parseBoolean(active));
			priceBean.setUnitCode(unitCode);
			priceBean.setPriceDate(new Date ());
			priceBean.setProduct(prodCrud.getById(productId));
			System.out.println(priceBean.toString());
			crud.insert(priceBean);
			}
			else {
			priceBean = crud.getById(Integer.parseInt(editId));
			priceBean.setPrice(Double.parseDouble(price));
			priceBean.setActive(Boolean.parseBoolean(active));
			priceBean.setUnitCode(unitCode);
			priceBean.setPriceDate(new Date ());
			priceBean.setProduct(prodCrud.getById(productId));
			crud.update(priceBean);
		} 
		
	} catch (PersistencyException e) {
		e.printStackTrace();
	}
	catch (Exception ex) {
		ex.printStackTrace();
	}	
			return "SUCCESS";
			
		}
}
